{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# SymbolicConditional"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A `SymbolicConditional` represents a conditional probability distribution P(Frontals | Parents) in purely symbolic form. It only stores the keys of the frontal variables and the parent variables, without any associated numerical probability function.\n",
        "\n",
        "`SymbolicConditional` objects are typically produced as the result of symbolic elimination on a `SymbolicFactorGraph`. A collection of these forms a `SymbolicBayesNet`."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/symbolic/doc/SymbolicConditional.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "tags": [
          "remove-cell"
        ]
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Note: you may need to restart the kernel to use updated packages.\n"
          ]
        }
      ],
      "source": [
        "%pip install --quiet gtsam-develop"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from gtsam import SymbolicConditional\n",
        "from gtsam.symbol_shorthand import X, L"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Creating SymbolicConditionals\n",
        "\n",
        "Conditionals specify frontal (conditioned) variables and parent variables."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "P(X(0)):  P( x0)\n",
            " Nr Frontals: 1, Nr Parents: 0\n",
            "\n",
            "P(X(1) | X(0)):  P( x1 | x0)\n",
            " Nr Frontals: 1, Nr Parents: 1\n",
            "\n",
            "P(X(2) | X(0), X(1)):  P( x2 | x0 x1)\n",
            " Nr Frontals: 1, Nr Parents: 2\n",
            "\n",
            "P(L(1) | X(0), X(1)):  P( l1 | x0 x1)\n",
            " Nr Frontals: 1, Nr Parents: 2\n",
            "\n",
            "P(X(2), L(1) | X(0), X(1)):  P( x2 l1 | x0 x1)\n",
            " Nr Frontals: 2, Nr Parents: 2\n"
          ]
        }
      ],
      "source": [
        "# P(X(0))\n",
        "c0 = SymbolicConditional(X(0))\n",
        "c0.print(\"P(X(0)): \")\n",
        "print(f\" Nr Frontals: {c0.nrFrontals()}, Nr Parents: {c0.nrParents()}\\n\")\n",
        "\n",
        "# P(X(1) | X(0))\n",
        "c1 = SymbolicConditional(X(1), X(0))\n",
        "c1.print(\"P(X(1) | X(0)): \")\n",
        "print(f\" Nr Frontals: {c1.nrFrontals()}, Nr Parents: {c1.nrParents()}\\n\")\n",
        "\n",
        "# P(X(2) | X(0), X(1))\n",
        "c2 = SymbolicConditional(X(2), X(0), X(1))\n",
        "c2.print(\"P(X(2) | X(0), X(1)): \")\n",
        "print(f\" Nr Frontals: {c2.nrFrontals()}, Nr Parents: {c2.nrParents()}\\n\")\n",
        "\n",
        "# P(L(1) | X(0), X(1))\n",
        "c3 = SymbolicConditional(L(1), X(0), X(1))\n",
        "c3.print(\"P(L(1) | X(0), X(1)): \")\n",
        "print(f\" Nr Frontals: {c3.nrFrontals()}, Nr Parents: {c3.nrParents()}\\n\")\n",
        "\n",
        "# Create from keys and number of frontals, e.g. P(X(2), L(1) | X(0), X(1))\n",
        "# Keys = [Frontals..., Parents...]\n",
        "c4 = SymbolicConditional.FromKeys([X(2), L(1), X(0), X(1)], 2)\n",
        "c4.print(\"P(X(2), L(1) | X(0), X(1)): \")\n",
        "print(f\" Nr Frontals: {c4.nrFrontals()}, Nr Parents: {c4.nrParents()}\")"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "py312",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
